/*
 *  Copyright 2010 dmakariev.
 * 
 */
package org.deltaset.faces.upload;

import javax.el.ELContext;
import javax.el.ValueExpression;
import javax.faces.component.UINamingContainer;
import javax.faces.context.FacesContext;

/**
 *
 * @author dmakariev
 */
//@FacesComponent(value = "compositeStreamer")
public class StreamerNamingContainer extends UINamingContainer {

    public StreamerNamingContainer() {
    }

    @Override
    public String getFamily() {
        return "javax.faces.NamingContainer";
    }
    private DsFileItem cachedCurrentFile;

    private DsFileItem getCurrentFile() {
        if (null == cachedCurrentFile) {
            final ValueExpression valueVE = this.getValueExpression("value");
            final ELContext currentELContext = FacesContext.getCurrentInstance().getELContext();
            cachedCurrentFile = (DsFileItem) valueVE.getValue(currentELContext);
        }
        return cachedCurrentFile;
    }

    public String getPrepareUrl() {
        final DsFileItem currentFile = getCurrentFile();
        return StreamPhaseListener.prepareUrl(currentFile);
    }

    public boolean isImage() {
        final DsFileItem currentFile = getCurrentFile();
        if(null==currentFile){
            return false;
        }
        final String contentType = currentFile.getContentType();
        if(null==contentType){
            return false;
        }
        final boolean isImage = contentType.contains("image");
        return isImage;    
    }
}
